/*
 * utils.cpp
 *
 *  Created on: Mar 29, 2010
 *      Author: Lukasz Spintzyk
 */

#include "utils.h"
#include <string>
#include <cctype>

std::string remove_dots(const std::string& value) {
	std::string out = value;

	uint32_t pos = 0;
	for (std::string::const_iterator cit = value.begin();
		cit != value.end(); ++cit)
	{
		if (*cit != ':')
		{
			out[pos] = *cit;
			pos++;
		}
	}
	out[pos] = '\0';
	return out;
}

std::string findLastName(const std::string& path)
{
	size_t last_pos = 0;
	for (std::string::const_reverse_iterator rcit = path.rbegin();
			rcit != path.rend();
			++rcit)
	{
		if (*rcit == '/')
		{
			return std::string(rcit.base(), path.end());
		}
		++last_pos;
	}

	return path;
}

std::string getParentDir(const std::string& path)
{
	std::string child_dir = findLastName(path);
	return path.substr(0, path.length() - child_dir.length());
}

bool execute_ok(boost::shared_ptr<Inode> inode, uint16_t uid, uint16_t gid) {

	if (uid == inode->d_uid) {
		if (inode->d_mode & S_IXUSR) {
			return true;
		}
	}

	if (gid == inode->d_gid) {
		if (inode->d_mode & S_IXGRP) {
			return true;
		}
	}

	if (inode->d_mode & S_IXOTH) {
		return true;
	}

	return false;

}

bool read_ok(boost::shared_ptr<Inode> inode, uint16_t uid, uint16_t gid)
{

	if (uid == inode->d_uid) {
		if (inode->d_mode & S_IRUSR) {
			return true;
		}
	}

	if (gid == inode->d_gid) {
		if (inode->d_mode & S_IRGRP) {
			return true;
		}
	}

	if (inode->d_mode & S_IROTH) {
		return true;
	}

	return false;
}

